package cn.bugstack.mybatis.scripting.xmltags;

/**
 * process <if></if> tag
 */
public class IfSqlNode implements SqlNode {

    private ExpressionEvaluator evaluator;

    private  MixedSqlNode mixedSqlNode;

    private  String test;

    public IfSqlNode(MixedSqlNode mixedSqlNode, String test) {
        this.mixedSqlNode = mixedSqlNode;
        this.test = test;
        this.evaluator = new ExpressionEvaluator();
    }

    @Override
    public boolean apply(DynamicContext context) {
        // 如果满足条件，则apply，并返回true
        if (evaluator.evaluateBoolean(test, context.getBindings())) {
            mixedSqlNode.apply(context);
            return true;
        }
        return false;
    }
}
